package wassalt.Utils;

import wassalt.Services.HTTPService;
import wassalt.client.R;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class ClientServerCommunication {

	static HTTPReceiver receiver;
	static IntentFilter filter;
	
	/**
	 * Constructor -- Initializes the filter and the HTTPReceiver
	 * @author Aya Kamel
	 * @param mCtx
	 * 		The current environment context
	 */
	public ClientServerCommunication(Context mCtx)
	{
		filter = new IntentFilter();
		filter.addAction(AppConstants.HTTP_DELETE_FILTER);
		filter.addAction(AppConstants.HTTP_CANCEL_FILTER);
		filter.addAction(AppConstants.HTTP_STATUS);
		filter.addAction(AppConstants.HTTP_IN_FILTER);
		filter.addAction(AppConstants.HTTP_OUT_FILTER);
		filter.addAction(AppConstants.HTTP_SELF_FILTER);
		filter.addAction(AppConstants.HTTP_GETARRIVALS_FILTER);
		filter.addAction(AppConstants.HTTP_CHECK_FILTER);
		DbAdapter db = new DbAdapter(mCtx);
		receiver = new HTTPReceiver(mCtx, db);
	}
	
	/**
	 * Unregisters the HTTP receiver
	 * @author Aya Kamel
	 * @param mCtx
	 * 		The current environment context
	 */
	public static void unRegister(Context mCtx)
	{
		mCtx.unregisterReceiver(receiver);
	}
	
	/**
	 * Initiates an HTTP Service call to delete the request with id req_id from the server
	 * @author Aya Kamel
	 * @param req_id 
	 * 		The id of the request to be deleted
	 */
	public static void deleteRequestFromServer(String req_id, Context mCtx,
			long rowId, int table) {
		mCtx.registerReceiver(receiver, filter);
		String PATH = mCtx.getString(R.string.server_url)+AppConstants.DELETE_URL;
		Intent serviceIntent = new Intent(mCtx, HTTPService.class);
		serviceIntent.setAction(AppConstants.HTTP_DELETE_FILTER);
		serviceIntent.putExtra(AppConstants.URL, PATH);
		serviceIntent.putExtra("notif_id", req_id);
		serviceIntent.putExtra("phone", User.registeredPhone(mCtx));
		serviceIntent.putExtra("rowId", rowId);
		serviceIntent.putExtra("table", table);
		mCtx.startService(serviceIntent);
	}
	
	/**
	 * Initiates an HTTP Service call to cancel the request with id req_id from the server
	 * @author Aya Kamel
	 * @param req_id 
	 * 		The id of the request to be cancelled
	 */
	public static void cancelRequestFromServer(String req_id, Context mCtx, 
			long rowId, int table) {
		mCtx.registerReceiver(receiver, filter);
		String PATH = mCtx.getString(R.string.server_url)+AppConstants.CANCEL_URL;
		Intent serviceIntent = new Intent(mCtx, HTTPService.class);
		serviceIntent.setAction(AppConstants.HTTP_CANCEL_FILTER);
		serviceIntent.putExtra(AppConstants.URL, PATH);
		serviceIntent.putExtra("notif_id", req_id);
		serviceIntent.putExtra("rowId", rowId);
		serviceIntent.putExtra("table", table);
		mCtx.startService(serviceIntent);
	}
	
	/**
	 * Initiates an HTTPService call to update the status of request with id to b
	 * @author Aya Kamel
	 * @param id
	 *            The id of the notification to be edited
	 * @param b
	 *            true if accepted and false if rejected Initiates an HTTP
	 *            
	 */
	public static void setStatus(String req_id, boolean b, Context mCtx, 
			long rowId) {
		// Get the server id of the notification selected
		mCtx.registerReceiver(receiver, filter);
		String PATH = mCtx.getString(R.string.server_url);
		String TAG = "";
		if (b)
		{
			PATH += AppConstants.SETACCEPTED_URL;
			TAG = "notif_id";
		}
		else
		{
			PATH += AppConstants.SETREJECTED_URL;
			TAG = "req_id";
		}

		Intent serviceIntent = new Intent(mCtx, HTTPService.class);
		serviceIntent.setAction(AppConstants.HTTP_STATUS);
		serviceIntent.putExtra(AppConstants.URL, PATH);
		serviceIntent.putExtra(TAG, req_id);
		serviceIntent.putExtra("rowId", rowId);
		serviceIntent.putExtra("value", b);
		mCtx.startService(serviceIntent);
	}
	
	/**
	 * Initiates an HTTPService call to refresh the incoming notifications database
	 * @author Aya Kamel 
	 */
	public static void refreshIncomingNotifications(Context mCtx) {

		mCtx.registerReceiver(receiver, filter);
		String PATH = mCtx.getString(R.string.server_url)+ AppConstants.REQUESTS_URL;
		Intent serviceIntent = new Intent(mCtx, HTTPService.class);
		serviceIntent.setAction(AppConstants.HTTP_IN_FILTER);
		serviceIntent.putExtra(AppConstants.URL, PATH);
		serviceIntent.putExtra("id", User.registeredId(mCtx));
		mCtx.startService(serviceIntent);
	}
	
	/**
	 * Initiates an HTTPService call to refresh the outgoing notifications database
	 * @author Aya Kamel 
	 */
	public static void refreshOutgoingNotifications(Context mCtx) {

		mCtx.registerReceiver(receiver, filter);
		String PATH = mCtx.getString(R.string.server_url)+ AppConstants.OFFERS_URL;
		Intent serviceIntent = new Intent(mCtx, HTTPService.class);
		serviceIntent.setAction(AppConstants.HTTP_OUT_FILTER);
		serviceIntent.putExtra(AppConstants.URL, PATH);
		serviceIntent.putExtra("id", User.registeredId(mCtx));
		mCtx.startService(serviceIntent);
	}
	
	/**
	 * Initiates an HTTPService call to refresh the self notifications database
	 * @author Aya Kamel 
	 */
	public static void refreshSelfNotifications(Context mCtx) {

		mCtx.registerReceiver(receiver, filter);
		String PATH = mCtx.getString(R.string.server_url) + AppConstants.ALARM_URL ;
		Intent serviceIntent = new Intent(mCtx, HTTPService.class);
		serviceIntent.setAction(AppConstants.HTTP_SELF_FILTER);
		serviceIntent.putExtra(AppConstants.URL, PATH);
		serviceIntent.putExtra("id", User.registeredId(mCtx));
		mCtx.startService(serviceIntent);
	}
	
	/**
	 * Initiates an HTTPService call to retrieve the fulfilled requests
	 * @author Aya Kamel 
	 */
	public static void getArrivals(Context mCtx) {

		mCtx.registerReceiver(receiver, filter);
		String PATH = mCtx.getString(R.string.server_url) + AppConstants.GETARRIVALS_URL;
		Intent serviceIntent = new Intent(mCtx, HTTPService.class);
		serviceIntent.setAction(AppConstants.HTTP_GETARRIVALS_FILTER);
		serviceIntent.putExtra(AppConstants.URL, PATH);
		serviceIntent.putExtra("phone", User.registeredPhone(mCtx));
		mCtx.startService(serviceIntent);
	}
	
	/**
	 * Initiates an HTTPService call to retrieve the new requests
	 * @author Aya Kamel 
	 */
	public static void getNewRequest(Context mCtx) {

		mCtx.registerReceiver(receiver, filter);
		String PATH = mCtx.getString(R.string.server_url) + AppConstants.CHECK_URL;
		Intent serviceIntent = new Intent(mCtx, HTTPService.class);
		serviceIntent.setAction(AppConstants.HTTP_CHECK_FILTER);
		serviceIntent.putExtra(AppConstants.URL, PATH);
		serviceIntent.putExtra(AppConstants.PHONE, User.registeredPhone(mCtx));
		mCtx.startService(serviceIntent);
	}
}
